/*
 * Copyright 2008 Zoundry LLC, Inc.  All Rights Reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 */
package org.zoundry.gaedt.core.models;

import java.util.HashMap;
import java.util.Map;

import javax.xml.namespace.QName;

/**
 * Base class for all ZGae models.
 */
public class ZGaeModel
{
    /** This model's parent. */
    private ZGaeModel mParent;
    /** Where this model starts in the source file. */
    private int mStartLine;
    /** Where this model starts in the source file. */
    private int mStartCol;
    /** Where this model ends in the source file. */
    private int mEndLine;
    /** Where this model ends in the source file. */
    private int mEndCol;
    /** Map of prefix to namespace. */
    private Map<String, String> mNamespaceMap;

    /**
     * Default c'tor.
     */
    public ZGaeModel()
    {
        setNamespaceMap(new HashMap<String, String>());
    }

    /**
     * @return the startLine
     */
    public int getStartLine()
    {
        return mStartLine;
    }

    /**
     * @param aStartLine the startLine to set
     */
    public void setStartLine(int aStartLine)
    {
        mStartLine = aStartLine;
    }

    /**
     * @return the startCol
     */
    public int getStartCol()
    {
        return mStartCol;
    }

    /**
     * @param aStartCol the startCol to set
     */
    public void setStartCol(int aStartCol)
    {
        mStartCol = aStartCol;
    }

    /**
     * @return the endLine
     */
    public int getEndLine()
    {
        return mEndLine;
    }

    /**
     * @param aEndLine the endLine to set
     */
    public void setEndLine(int aEndLine)
    {
        mEndLine = aEndLine;
    }

    /**
     * @return the endCol
     */
    public int getEndCol()
    {
        return mEndCol;
    }

    /**
     * @param aEndCol the endCol to set
     */
    public void setEndCol(int aEndCol)
    {
        mEndCol = aEndCol;
    }

    /**
     * Adds a namespace mapping.
     *
     * @param aPrefix
     * @param aNamespace
     */
    public void addNamespaceMapping(String aPrefix, String aNamespace)
    {
        getNamespaceMap().put(aPrefix, aNamespace);
    }

    /**
     * Gets a namespace for the given prefix.
     *
     * @param aPrefix
     */
    public String getNamespaceMapping(String aPrefix)
    {
        return getNamespaceMap().get(aPrefix);
    }

    /**
     * Resolves the given prefix to a namespace.
     *
     * @param aPrefix
     */
    public String resolvePrefix(String aPrefix)
    {
        String namespace = getNamespaceMapping(aPrefix);
        if (namespace != null)
            return namespace;
        else if (getParent() != null)
            return getParent().resolvePrefix(aPrefix);
        else
            return null;
    }

    /**
     * Decodes the given encoded QName within the context of this
     * model.
     *
     * @param aEncodedQName
     */
    public QName resolveEncodedQName(String aEncodedQName)
    {
        String[] strings = aEncodedQName.split(":");
        if (strings.length == 1)
            return new QName("", aEncodedQName);
        String prefix = strings[0];
        String localName = strings[1];
        String namespace = resolvePrefix(prefix);
        return new QName(namespace, localName);
    }

    /**
     * @return the namespaceMap
     */
    protected Map<String, String> getNamespaceMap()
    {
        return mNamespaceMap;
    }

    /**
     * @param aNamespaceMap the namespaceMap to set
     */
    protected void setNamespaceMap(Map<String, String> aNamespaceMap)
    {
        mNamespaceMap = aNamespaceMap;
    }

    /**
     * @return the parent
     */
    public ZGaeModel getParent()
    {
        return mParent;
    }

    /**
     * @param aParent the parent to set
     */
    public void setParent(ZGaeModel aParent)
    {
        mParent = aParent;
    }
}
